#!/bin/sh
# prettier pre-commit hook for git

get_list_of_files() {
    git diff --cached --name-only --diff-filter=ACMRTUXB | grep -E '\.(js|vue)$'
}

main() {
    local files=$(get_list_of_files)
    if [ -z "${files}" ]
    then
        exit 0
    fi
    local prettier_output
    prettier_output=$(npm run prettier --silent -- --list-different ${files})
    local status=$?
    if [ ${status} -ne 0 ]
    then
        local formatted_output=$(tr '\n' ' ' <<< $prettier_output)
        local error_message="The following files do not respect coding standards:

$prettier_output

You can run the following command to format them automatically:
npm run prettier -- --write $formatted_output
"
        printf "$error_message"
    fi

    exit ${status}
}
main
